package com.example.recyclerviewproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

//定义适配器：Adapter从模型层获取数据，然后提供给RecyclerView显示，是沟通的桥梁。
public class MyAdapter3  extends RecyclerView.Adapter<MyAdapter3.MyViewHolder> {


    //上下文
    private Context mContext;
    //数据集合
    private List<ItemBean> mList;
    //inflater
    private  LayoutInflater inflater;

    public MyAdapter3(List<ItemBean> list,Context context){
        this.mList=list;
        this.mContext=context;
         inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //生成item的view
        View itemView = inflater.inflate(R.layout.list_item_layout3, viewGroup, false);
        //创建ViewHolder
        MyViewHolder viewHolder = new MyViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
        ItemBean itemBean = mList.get(position);
        myViewHolder.title_tv.setText(itemBean.getTitle());
        myViewHolder.content_tv.setText(itemBean.getContent());
        myViewHolder.icon_img.setImageResource(itemBean.getImageInt());
        myViewHolder.root_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, itemBean.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends  RecyclerView.ViewHolder{
        private ImageView icon_img;
        private TextView title_tv;
        private TextView content_tv;
        private RelativeLayout root_layout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_img= itemView.findViewById(R.id.icon_img);
            title_tv= itemView.findViewById(R.id.title_tv);
            content_tv= itemView.findViewById(R.id.content_tv);
             root_layout = itemView.findViewById(R.id.root_layout);
        }
    }

}
